<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@page import="java.util.*,com.oreilly.servlet.MultipartRequest,java.io.*,com.database.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String filePath =this.getServletContext().getRealPath("")+"/upload";
		MultipartRequest theMultipartRequest = new  MultipartRequest(request,filePath,10*1024*1024) ; 
		Enumeration theEnumeration = theMultipartRequest.getFileNames() ;
		String title,content,datetime,image="",type;
		int times;
		if(theEnumeration.hasMoreElements()){//仅仅一个图像
			String fieldName = (String)theEnumeration.nextElement() ; 
			String fileName =theMultipartRequest.getFilesystemName(fieldName) ;              
			File theFile = theMultipartRequest.getFile(fieldName) ;        
			image=fileName;
		}
		if(image==null){
			image="NO";
		}
		title = theMultipartRequest.getParameter("title");
		byte b[]=title.getBytes("ISO8859_1");
		title=new String(b,"utf-8");
		content = theMultipartRequest.getParameter("content");
		b=content.getBytes("ISO8859_1");
		content=new String(b,"utf-8");
		type=theMultipartRequest.getParameter("type");
		b=type.getBytes("ISO8859_1");
		type=new String(b,"utf-8");
		Date date=new Date();
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date currTime = new java.util.Date();
		datetime = formatter.format(currTime);
		times=0;
		String sql="insert into news(title,content,datetime,times,image,type) values('"+title+"','"+content+"','"+datetime+"','"+times+"','"+image+"','"+type+"')";
		DBAccess db=new DBAccess();
		if(db.createConn()){
			if(db.update(sql)){
				out.println("发表新闻成功");
				response.setHeader("refresh", "0.5;url=viewnews.jsp");
			 }else{
				out.print("发布新闻失败");
			 }
		}else{
		 	out.print("数据库链接失败！");
		}
	%>
</body>
</html>